//
// Created by v on 19-6-10.
//

#include "CMObserver.h"

int CMObserver::registerObserver(CMObserver *obs)
{
    m_objs.push_back(obs);
    return m_objs.size();
}

int CMObserver::unregisterObserver(CMObserver *obs)
{
    bool removed = false;
#ifdef GCC_HIGH
    for (auto iter = m_objs.begin(); iter != m_objs.end();)
    {
#else
    std::list<CMObserver *>::iterator iter;
    for (iter = m_objs.begin(); iter != m_objs.end();)
    {
#endif
        if (*iter == obs)
        {
#ifdef GCC_HIGH
            auto titer = iter++;
#else
            std::list<CMObserver *>::iterator titer = iter++;
#endif
            m_objs.erase(titer);
            removed = true;
        }
        else
        {
            iter++;
        }
    }
    return removed ? 0 : -1;
}

int CMObserver::updateAll(void *args)
{
#ifdef GCC_HIGH
    for (auto iter : m_objs)
    {
        CMObserver *o = iter;
#else
    for (std::list<CMObserver *>::iterator iter = m_objs.begin();
         iter != m_objs.end();)
    {
        CMObserver *o = *iter;
#endif
        o->statusChanged(args);
    }
    return m_objs.size();
}

int CMObserver::statusChanged(void *args)
{
    // listen implement it
    return -1;
}
